package com.huarui.saleReturn.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.saleReturn.dao.ISaleReturnItemDao;
import com.huarui.saleReturn.model.SaleReturnItem;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class SaleReturnItemDao extends BaseDaoImpl implements
		ISaleReturnItemDao {

	@Override
	public void batchDeleteItems(Map<String, Object> map) {
		getSqlMapClientTemplate().delete(namespace + ".deleteByIds", map);
	}

	@Override
	public void batchUpdateItems(final List<SaleReturnItem> saleReturnItems) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor)throws SQLException {
				executor.startBatch();
				int batch = 0;
				for (SaleReturnItem saleReturnItem : saleReturnItems) {
					executor.update(namespace + ".update", saleReturnItem);
					batch++;
					if (batch == 100) {
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});

	}

	@Override
	public void batchInsertItems(final List<SaleReturnItem> saleReturnItems) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor)throws SQLException {
				executor.startBatch();
				int batch = 0;
				for (SaleReturnItem saleReturnItem : saleReturnItems) {
					executor.insert(namespace + ".create", saleReturnItem);
					batch++;
					if (batch == 100) {
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getSaleReturnItemsForMapBySaleReturnId(Integer saleReturnId) {
		return getSqlMapClientTemplate().queryForList(namespace+".getSaleReturnItemsBySaleReturnId", saleReturnId);
		
	}

	@Override
	public Integer getSaleReturnItemCountByConditions(Map<String, Object> queryMap) {
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".getSaleReturnItemCountByConditions", queryMap);		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map> getSaleReturnItemForMapByConditions(Map<String, Object> queryMap) {
		return getSqlMapClientTemplate().queryForList(namespace+".getSaleReturnItemForMapByConditions", queryMap);
	}

}
